// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin;

import java.io.PrintWriter;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.util.ASMifierClassVisitor;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class BitJumpTest {
  private static final int STEPS = 35000000;

  public static void main(String[] args) throws Exception {
    long time;
    int a;

    for (int n = 0; n < 2; n++) {
      time = System.currentTimeMillis();
      a = 0;
      for (int i = 0; i < STEPS; i++) {
        int x = i;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
        if (x < 0) a++; x <<= 1;
      }
      time = System.currentTimeMillis() - time;
      System.out.println("1) Time: " + time + "ms, result: " + a);
  
      time = System.currentTimeMillis();
      a = 0;
      for (int i = 0; i < STEPS; i++) {
        int x = i;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
        if ((x & 0x80000000) != 0) a++; x <<= 1;
      }
      time = System.currentTimeMillis() - time;
      System.out.println("2) Time: " + time + "ms, result: " + a);
  
      time = System.currentTimeMillis();
      a = 0;
      for (int i = 0; i < STEPS; i++) {
        int x = i;
        if ((x & 0x80000000) != 0) a++;
        if ((x & 0x40000000) != 0) a++;
        if ((x & 0x20000000) != 0) a++;
        if ((x & 0x10000000) != 0) a++;
        if ((x & 0x8000000) != 0) a++;
        if ((x & 0x4000000) != 0) a++;
        if ((x & 0x2000000) != 0) a++;
        if ((x & 0x1000000) != 0) a++;
        if ((x & 0x800000) != 0) a++;
        if ((x & 0x400000) != 0) a++;
        if ((x & 0x200000) != 0) a++;
        if ((x & 0x100000) != 0) a++;
        if ((x & 0x80000) != 0) a++;
        if ((x & 0x40000) != 0) a++;
        if ((x & 0x20000) != 0) a++;
        if ((x & 0x10000) != 0) a++;
        if ((x & 0x8000) != 0) a++;
        if ((x & 0x4000) != 0) a++;
        if ((x & 0x2000) != 0) a++;
        if ((x & 0x1000) != 0) a++;
        if ((x & 0x800) != 0) a++;
        if ((x & 0x400) != 0) a++;
        if ((x & 0x200) != 0) a++;
        if ((x & 0x100) != 0) a++;
        if ((x & 0x80) != 0) a++;
        if ((x & 0x40) != 0) a++;
        if ((x & 0x20) != 0) a++;
        if ((x & 0x10) != 0) a++;
        if ((x & 0x8) != 0) a++;
        if ((x & 0x4) != 0) a++;
        if ((x & 0x2) != 0) a++;
        if ((x & 0x1) != 0) a++;
      }
      time = System.currentTimeMillis() - time;
      System.out.println("3) Time: " + time + "ms, result: " + a);
  
      time = System.currentTimeMillis();
      a = 0;
      for (int i = 0; i < STEPS; i++) {
        int x = i;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
        if ((x & 0x80000000) == 0x80000000) a++; x <<= 1;
      }
      time = System.currentTimeMillis() - time;
      System.out.println("4) Time: " + time + "ms, result: " + a);
  
  
      time = System.currentTimeMillis();
      a = 0;
      for (int i = 0; i < STEPS; i++) {
        int x = i;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
        if ((x & 1) != 0) a++; x >>= 1;
      }
      time = System.currentTimeMillis() - time;
      System.out.println("5) Time: " + time + "ms, result: " + a);
  
      time = System.currentTimeMillis();
      a = 0;
      for (int i = 0; i < STEPS; i++) {
        int b00 = i & 0x1;
        int b01 = i & 0x2;
        int b02 = i & 0x4;
        int b03 = i & 0x8;
        int b04 = i & 0x10;
        int b05 = i & 0x20;
        int b06 = i & 0x40;
        int b07 = i & 0x80;
        int b10 = i & 0x100;
        int b11 = i & 0x200;
        int b12 = i & 0x400;
        int b13 = i & 0x800;
        int b14 = i & 0x1000;
        int b15 = i & 0x2000;
        int b16 = i & 0x4000;
        int b17 = i & 0x8000;
        int b20 = i & 0x10000;
        int b21 = i & 0x20000;
        int b22 = i & 0x40000;
        int b23 = i & 0x80000;
        int b24 = i & 0x100000;
        int b25 = i & 0x200000;
        int b26 = i & 0x400000;
        int b27 = i & 0x800000;
        int b30 = i & 0x1000000;
        int b31 = i & 0x2000000;
        int b32 = i & 0x4000000;
        int b33 = i & 0x8000000;
        int b34 = i & 0x10000000;
        int b35 = i & 0x20000000;
        int b36 = i & 0x40000000;
        int b37 = i & 0x80000000;
        
        if (b00 != 0) a++;
        if (b01 != 0) a++;
        if (b02 != 0) a++;
        if (b03 != 0) a++;
        if (b04 != 0) a++;
        if (b05 != 0) a++;
        if (b06 != 0) a++;
        if (b07 != 0) a++;
        if (b10 != 0) a++;
        if (b11 != 0) a++;
        if (b12 != 0) a++;
        if (b13 != 0) a++;
        if (b14 != 0) a++;
        if (b15 != 0) a++;
        if (b16 != 0) a++;
        if (b17 != 0) a++;
        if (b20 != 0) a++;
        if (b21 != 0) a++;
        if (b22 != 0) a++;
        if (b23 != 0) a++;
        if (b24 != 0) a++;
        if (b25 != 0) a++;
        if (b26 != 0) a++;
        if (b27 != 0) a++;
        if (b30 != 0) a++;
        if (b31 != 0) a++;
        if (b32 != 0) a++;
        if (b33 != 0) a++;
        if (b34 != 0) a++;
        if (b35 != 0) a++;
        if (b36 != 0) a++;
        if (b37 != 0) a++;
      }
      time = System.currentTimeMillis() - time;
      System.out.println("6) Time: " + time + "ms, result: " + a);
    }
    /*
    ClassReader classReader = new ClassReader(BitJumpTest.class.getName());
    ClassVisitor asmifier = new ASMifierClassVisitor(new PrintWriter(System.out));
    classReader.accept(asmifier, ClassReader.SKIP_DEBUG);
    */
  }
}
